<?php
namespace NoteManage\Controller;

use Think\Controller;
use Think\Model;

class Test
{
	public $code;
	public $msg;
	public $count;
	public $data;
}

class NoteController extends Controller
{
    public function getmsg($page,$limit)//返回所有学习记录信息
    {
        $test=new Test();
		$note=M('Note');
		$start=($page-1)*$limit;
        $info = $note
            ->join('left join sz_user ON sz_note.edit=sz_user.id')
            ->field('sz_note.title as title,sz_note.content as content,sz_note.time as time,sz_user.name as edit,sz_note.file as file,sz_note.id as id,sz_note.score as score')
            ->order('id desc')
            ->limit($start,$limit)
            ->select();//遍历note
        $count = $note->count();
        $test->code = 0;
        $test->msg = "";
        $test->count = $count;
        $test->data = $info;
        $this->ajaxReturn($test);
    }

    public function showNote()//显示所有学习记录
    {
        if(session('status')>0)//权限验证
        {
            $this->display();//显示页面
        }
    }

    public function showAddNote()//显示页面
    {
        if(session('status')>0)//权限验证
        {
            $this->display('addNote');//显示页面
        }
    }

    public function addNote()//添加学习记录
    {
        if(!empty($_POST))
        {
            $note = D("Note");//实例化Note对象
            if ($note->create())//创建数据对象
            {
                $note->edit = session('id');//设置作者
                $note->file = I('post.startname');//设置文件的上传名
                $note->savefile = I('post.savename');//设置文件的保存名
                $info=$note->add();//将数据存入数据库
                if ($info)//添加成功
                {
                    $return['status']=1;
                    $this->ajaxReturn($return);
                } 
                else//添加失败
                {  
                    $return['status']=0;
                    $return['info']='添加失败';
                    $this->ajaxReturn($return);//返回错误信息  
                }
            } 
            else //创建数据对象错误
            {
                $return['status']=0;
                $return['info']=$note->getError();
                $this->ajaxReturn($return);//返回错误信息
            }
        }
    }
 
    public function upload()//文件上传
    {
        $Upload = new \Think\Upload();//实例化上传类
        $Upload->autoSub = false;
        $Upload->rootPath = './Public/Note/';//设置附件上传目录
        $Upload->savePath = '';//附件上传子目录
        $info = $Upload->upload();
        foreach ($info as $file) {
            $savename = $file['savename'];
            $startname = $file['name'];
        }
        $date['startname']=$startname;
        $date['savename']=$savename;
        $this->ajaxReturn($date);//返回文件上传信息
    }

    public function deleteNote()//删除学习记录
    {
        if (!empty($_GET)) 
        {
            if (session('status') == 4)//管理员有权删除学习记录
            {
                $where['id'] = I('get.id');
                $note = M('Note');
                $result = $note->where($where)->delete();//删除该条学习记录在数据库中的信息
                if ($result) //删除成功
                {
                    $return['status']=1;
                    $return['info']='删除成功';
                    $this->ajaxReturn($return);
                } 
                else //删除失败
                {
                    $return['status']=0;
                    $return['info']='删除失败';
                    $this->ajaxReturn($return);//返回错误信息
                }
            } 
            else//学习记录上传者有权删除学习记录
            {
                $note = M('Note');
                $where['id'] = I('get.id');
                $result = $note->field('edit')->where($where)->find();
                if (session('id') == $result['edit']) //权限验证
                {
                    $where['id'] = I('get.id');
                    $note = M('Note');
                    $result = $note->where($where)->delete();//删除该条学习记录在数据库中的信息
                    if ($result) 
                    {
                        $return['status']=1;
                        $return['info']='删除成功';
                        $this->ajaxReturn($return);
                    } 
                    else 
                    {
                        $return['status']=0;
                        $return['info']='删除失败';
                        $this->ajaxReturn($return);//返回错误信息
                    }
                }
                $return['status']=0;
                $return['info']='权限不足';
                $this->ajaxReturn($return);//返回错误信息
            }
        }
    }

    public function showAlterNote()//显示修改页面
    {
        if(session('status')==4)//管理员有所有权限
        {
            $note = M('Note');
            $where['id'] = I('get.id');
            $result = $note->where($where)->find();
            $this->assign('result', $result);
            $this->display('alterNote');
        }
        else//学习记录上传人有权限
        {
            $note = M('Note');
            $where['id'] = I('get.id');
            $result = $note->field('edit')->where($where)->find();
            if (session('id') == $result['edit'])//权限验证
            {
                $note = M('Note');
                $where['id'] = I('get.id');
                $result = $note->where($where)->find();//查找相关信息
                $this->assign('result', $result);//渲染模板
                $this->display();//显示模板
            }
            else//权限不足
            {
                $this->error('权限不足');
            }
        }
    }

    public function getAlterNote()//回显要修改的值
    {
        if(!empty($_GET))
        {
            $note = M('Note');
            $where['id'] = I('get.id');
            $result = $note->where($where)->find();//查询相关信息
            $this->ajaxReturn($result);//返回相关信息
        }
    }

    public function alterNote()//修改学习记录
    {
        if (!empty($_GET))
        {      
            $note = D("Note");//实例化Note对象
            if ($note->create())//创建数据对象
            {
                $where['id'] = I('get.id');//学习记录的主键
                $note->title=I('post.title');//修改的数据
                $note->content=I('post.content');//修改的数据
                $note->time=I('post.time');//修改的数据
                $note->score=I('post.score');//修改的数据
                $note->file = I('post.startname');//设置文件的上传名
                $note->savefile = I('post.savename');//设置文件的保存名
                $info=$note->where($where)->save();//更改数据
                if ($info)//修改成功
                {
                    $return['status']=1;
                    $this->ajaxReturn($return);
                } 
                else //修改失败
                {
                    $return['status']=0;
                    $return['info']='修改失败';
                    $this->ajaxReturn($return);//返回错误信息
                }
            } 
            else //创建数据对象错误
            {
                $return['status']=0;
                $return['info']=$note->getError();
                $this->ajaxReturn($return);//返回错误信息
            }   
        }
    }

    public function showSelf()//显示个人学习记录页面
    {
        if(session('status')>0)//权限验证
        {
            $this->display();
        }
    }

    public function getSelf($page,$limit)//获取所有自己的学习记录
    {
        $test=new Test();//分页类
        $where['edit']=session('id');
		$note=M('Note');
		$start=($page-1)*$limit;
        $info = $note
            ->join('left join sz_user ON sz_note.edit=sz_user.id')
            ->field('sz_note.title as title,sz_note.content as content,sz_note.time as time,sz_user.name as edit,sz_note.file as file,sz_note.id as id,sz_note.score as score')
            ->where($where)
            ->order('id desc')
            ->limit($start,$limit)
            ->select();//遍历note
        $count = $note->count();
        $test->code = 0;
        $test->msg = "";
        $test->count = $count;
        $test->data = $info;
        $this->ajaxReturn($test);//返回分页对象
    }  
}